package whu.o2o.View;



import java.util.ArrayList;

import whu.o2o.util.ConstantData;
import whu.o2o.util.Utils;
import whu.o2o.vo.CityVO;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import whu.o2o.R;


/**
 * �ұߴ�����ĸ��ѯ��ListView
 * 
 * @author
 */
public class AlphabetListView extends FrameLayout {
	private Context mContext;
	private ListView mListView;
	private ArrayList<CityVO> mList;
	private LinearLayout alphabetLayout;
	private TextView mTextView;
	private Handler mHandler;
	private HideIndicator mHideIndicator = new HideIndicator();
	private int indicatorDuration = 1000;
	private final String FIRST_POSITION = "☆";

	private final class HideIndicator implements Runnable {
		@Override
		public void run() {
			mTextView.setVisibility(View.INVISIBLE);
		}
	}

	public AlphabetListView(Context context) {
		super(context);
		init(context);
	}

	public AlphabetListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public void setAdapter() {
		
	}

	private void init(Context context) {
		mContext = context;
		mHandler = new Handler();
		mListView = new ListView(mContext);
		mListView.setCacheColorHint(Color.TRANSPARENT);
		mListView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mListView.setVerticalFadingEdgeEnabled(false);
		mListView.setDivider(mContext.getResources().getDrawable(R.color.divider));
		mListView.setDividerHeight(1);
		initAlphabetLayout(mContext);
		mTextView = new TextView(mContext);
		mTextView.setTextSize(Utils.sp2px(mContext, 50));
		mTextView.setTextColor(Color.argb(150, 255, 255, 255));
		mTextView.setBackgroundColor(Color.argb(200, 0, 0, 0));
		mTextView.setMinWidth(Utils.dip2px(mContext, 70));
		mTextView.setMinHeight(Utils.dip2px(mContext, 70));
		int pixels = Utils.dip2px(mContext, 10);
		mTextView.setPadding(pixels, pixels, pixels, pixels);
		mTextView.setGravity(Gravity.CENTER);
		mTextView.setVisibility(View.INVISIBLE);
		FrameLayout.LayoutParams textLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		textLayoutParams.gravity = Gravity.CENTER;
		mTextView.setLayoutParams(textLayoutParams);
	}

	private void initAlphabetLayout(Context context) {
		// 创建字母布局
		alphabetLayout = new LinearLayout(context);
		alphabetLayout.setOrientation(LinearLayout.VERTICAL);
		FrameLayout.LayoutParams alphabetLayoutParams = new FrameLayout.LayoutParams(Utils.dip2px(mContext, 25), ViewGroup.LayoutParams.MATCH_PARENT);
		alphabetLayoutParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
		alphabetLayoutParams.rightMargin = Utils.dip2px(mContext, 1);
		alphabetLayout.setLayoutParams(alphabetLayoutParams);

		final String[] alphabet = new String[] { FIRST_POSITION, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "L", "S", "T", "U", "V", "W", "X", "Y", "Z" };
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		params.weight = 1;
		params.gravity = Gravity.CENTER_HORIZONTAL;
		for (int i = 0, count = alphabet.length; i < count; i++) {
			TextView textView = new TextView(context);
			textView.setTextColor(Color.argb(150, 150, 150, 150));
			textView.setTextSize(Utils.dip2px(mContext, 8));
			textView.setText(alphabet[i]);
			textView.setGravity(Gravity.CENTER);
			textView.setLayoutParams(params);
			textView.setTag(i + 1);
			alphabetLayout.addView(textView);
		}
		alphabetLayout.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					alphabetLayout.setBackgroundColor(Color.argb(50, 100, 200, 100));
					int l = (int) (event.getY() / (alphabetLayout.getHeight() / 26));
					if (l > 25)
						l = 25;
					else if (l < 0)
						l = 0;
					int pos = getPosition(alphabet[l]);
					if (pos != -1) {
						mTextView.setText(alphabet[l]);
						mTextView.setVisibility(View.VISIBLE);
						mHandler.removeCallbacks(mHideIndicator);
						mHandler.postDelayed(mHideIndicator, indicatorDuration);
						mListView.setSelection(pos);
					}
					break;
				case MotionEvent.ACTION_MOVE:
					l = (int) ((event.getY() + alphabetLayout.getHeight() / 26 / 2) / (alphabetLayout.getHeight() / 26));
					if (l > 25)
						l = 25;
					else if (l < 0)
						l = 0;
					pos = getPosition(alphabet[l]);
					if (pos != -1) {
						mTextView.setText(alphabet[l]);
						mTextView.setVisibility(View.VISIBLE);
						mHandler.removeCallbacks(mHideIndicator);
						mHandler.postDelayed(mHideIndicator, indicatorDuration);
						mListView.setSelection(pos);
					}
					break;
				case MotionEvent.ACTION_UP:
					alphabetLayout.setBackgroundResource(0);
					break;
				}
				return true;
			}
		});
	}

	private int getPosition(String letter) {
		if (letter.equals(FIRST_POSITION)) {
			return 0;
		}

		for (int i = 0; i < mList.size(); i++) {
			Character firstLetter = mList.get(i).getPinyin().charAt(0);
			if (firstLetter.toString().equals(letter.toLowerCase())) {
				return i;
			}
		}
		return -1;
	}
}